package com.example.wechat.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:encrypt
 * Function:微瓴接口调用（get，post）和签名生成
 * Reason:
 *
 * @Author: fengliang
 * @version:
 * @Date: Create in 上午 11:25 2019/7/4 0004
 */
@Component
public class WeLinkSignatureUtil {
        //实验室
//    private static final String appId="20009";
//    private static final String appKey="3387654Vh34J";
//    private static final String appTicket="304502210096374ad319c8dbae783031737ff648f71e15efd6db91f5f03fee24d9673dbb4802206bd33f8eb021777ba7b5917aed832ac466cc809dcfddc03d9e281a284d9d1933";
//    private static final String weLinkUrl="http://api.gzmtr.welink.qq.com";
//    private static final String projectId="A5B1D7E217A";


    //二期实验室
    private static final String appId="20013";
    private static final String appKey="GdH8mz682zq4";
    private static final String appTicket="30450220068c1b965a995dd7afcd424b542749f62442028fbe6cda7b52cc806a5fe93582022100c8f9ac266cf1e707818db5406c5f6f4ab601acdc2fa312a99650ce2ffbcc3697";
    private static final String weLinkUrl="http://172.23.10.90:8901";
    private static final String projectId="A5B1D7E217A";

    /**
     * 随机数对象
     */
    private static final Random random=new Random();
    @Resource(name = "redisUtil")
    private RedisUtil redisUtil;
    /**
     * 获取token跟ticket
     * @return map键值对
     */
    public  Map getTokenAndTicket(){
        Map<String, String> map = new HashMap<>();
        if(redisUtil.hasKey("laboratory_token")){
            System.out.println("进入具有缓存的:");
            Map weLinkResp = JSONObject.parseObject(redisUtil.get("laboratory_token"), Map.class);
            Map tokenData=(Map) weLinkResp.get("data");
            map.put("token", (String) tokenData.get("token"));
            map.put("iotim_ticket",(String)tokenData.get("iotim_ticket"));
            System.out.println(map);

        }else {
            System.out.println("进入不具有缓存的:");
            //获取当前时间
            Long timeStamp = DateUtils.getTimeStamp();
            //获取随机数
            Long randomNum = Long.parseLong(RandomCodeUtil.getRandNumber(5));
            //获取加密签名
            String sig =  autographUtil.encrypt(appKey,timeStamp,randomNum);
            //获取令牌
            String value =  HttpUtils.sendGet(weLinkUrl+"/common/ticket/loginByApp","appid="+appId + "&num=" + randomNum +
                    "&time=" + timeStamp + "&sig="+sig+ "&app_ticket=" + appTicket + "&projetId="+projectId);
            //转为map键值对
            Map weLinkResp = JSONObject.parseObject(value, Map.class);
            Map tokenData=(Map) weLinkResp.get("data");
            map.put("token", (String) tokenData.get("token"));
            map.put("iotim_ticket",(String)tokenData.get("iotim_ticket"));
            //将map存入redis
            redisUtil.setEx("laboratory_token",value,15, TimeUnit.MINUTES);
            System.out.println("==return ticket and token==");
        }
        return map;
    }


    /**
     * get调用微瓴接口
     *
     * @param url 无网址的接口访问地址  如：/common/ticket/loginByApp
     * @param map 除token跟ticket外的参数
     * @return
     */
    public  String weLinkHttpDoGet(String url,Map map){
        //获取令牌跟ticket
        Map tokenAndTicketMap = this.getTokenAndTicket();
        map.put("token", (String)tokenAndTicketMap.get("token"));
        map.put("iotim_ticket",(String)tokenAndTicketMap.get("iotim_ticket"));

        return HttpClientUtil.doGet(weLinkUrl+url, map);
    }

    /**
     * post调用微瓴接口
     *
     * @param url 无网址的接口访问地址  如：/common/ticket/loginByApp
     * @param map 除token跟ticket外的参数
     * @return
     */
    public  String weLinkHttpDoPost(String url,Map map){
        //获取令牌跟ticket
        Map tokenAndTicketMap = this.getTokenAndTicket();
//        map.put("token", (String) tokenAndTicketMap.get("token"));
//        map.put("iotim_ticket",(String)tokenAndTicketMap.get("iotim_ticket"));

        return HttpClientUtil.postByForm(weLinkUrl+url+"?token="+(String) tokenAndTicketMap.get("token"), map);
    }

    /**
     * json格式调用微瓴接口
     *
     * @param url 无网址的接口访问地址  如：/common/ticket/loginByApp
     * @param map
     * @return
     */
    public  String weLinkHttpDoJson(String url,Map map){
        //获取令牌跟ticket
        Map tokenAndTicketMap = this.getTokenAndTicket();
        return HttpClientUtil.postByJson(weLinkUrl+url+"?token="+(String) tokenAndTicketMap.get("token")+"&iotim_ticket="+(String)tokenAndTicketMap.get("iotim_ticket"), map);
    }


}
